package com.basemanage.controller.slide;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.basemanage.core.controller.JCBaseController;
import com.basemanage.core.model.Condition;
import com.basemanage.core.model.Operators;
import com.basemanage.core.util.CommonUtils;
import com.basemanage.core.util.DateUtils;
import com.basemanage.core.util.FileUtils;
import com.basemanage.core.util.JqGridModelUtils;
import com.basemanage.core.view.InvokeResult;
import com.basemanage.model.AppVersion;
import com.basemanage.model.SmSlide;
import com.basemanage.service.AppVersionService;
import com.basemanage.service.SlideService;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.xiaoleilu.hutool.json.JSONObject;
import com.oreilly.servlet.multipart.FileRenamePolicy;

public class SlideController extends JCBaseController {
	
	public void index() {
		render("slide_index.html");
	}
	
	/**
	 * 跳转（添加/编辑）页面
	 */
	public void addSlide(){
		Integer id=getParaToInt("id");
		String action="add";
		if(id!=null){
			this.setAttr("item", SlideService.dao.findById(id));
			action="edit";
		}
		this.setAttr("action", action);
		render("slide_add.html");
	}
	
	
	/**
	 * 数据分页列表
	 */
	public void getListData() {
			
		Page<SmSlide> pageInfo= SlideService.dao.getPage(this.getPage(), this.getRows(),this.getOrderby());
		this.renderJson(JqGridModelUtils.toJqGridView(pageInfo)); 
	}
	
	/**
	 * 保存数据
	 */
	public void saveSlide(){
		SmSlide slide=getModel(SmSlide.class,"obj");
		InvokeResult result= SlideService.me.saveSlide(slide);
		this.renderJson(result);
	}
	 
	/**
	 * 删除数据
	 */
	public void delSlide(){
		String ids=this.getPara("ids");
		InvokeResult result=SlideService.me.delSlide(ids);
		this.renderJson(result);
	}
	
	/*
	 * 显示设置
	 */
	public void setSlide(){
		
		
		render("slide_set.html");
		
	}
	
	/*
	 * 上传图片
	 */
	public void upload(){
		
		UploadFile uploadFile=this.getFile();
        String fileName=uploadFile.getOriginalFileName();
        

    }
	/*
	 * 上传文件
	 */
	public void uploadimg() {

	    UploadFile uploadFile = getFile();//在磁盘上保存文件

	    JSONObject data = new JSONObject();

	    StringBuffer URL= getRequest().getRequestURL();
	    String prefixString = URL.substring(0, 21);

        if (uploadFile != null) {
            String fileName = uploadFile.getFileName();
            String extentionName = fileName.substring(fileName
                    .lastIndexOf(".")); // 后缀名
         
            String newName =  prefixString + "/uploadfile/" + fileName;// 新名
//            String filePath = PathKit.getWebRootPath() + PropKit.get("uploadPath") + "/" + newName;// 文件完整路径
            uploadFile.getFile().renameTo(new File(newName)); // 重命名并上传文件

            //返回任意数据即代表上传成功
            data.put("filePath", newName);
            data.put("status", "success");
            renderJson(InvokeResult.success(data));
        }else {
            //返回任意数据即代表上传成功
        	data.put("error", "未选择文件");
        	data.put("status", "error");
            renderJson(InvokeResult.success(data));
        }
		
	}
	
	
}